//
//  TeachSimulationDeatilController.m
//  Coach_HaiGeEr
//
//  Created by liuchen on 2018/10/29.
//  Copyright © 2018年 海格尔. All rights reserved.
//

#import "TeachSimulationDeatilController.h"
#import "TeachSimulationCell.h"
#import "ExamPlayListModel.h"
#import "MJExtension.h"

@interface TeachSimulationDeatilController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableDictionary *dataDic;
@property (nonatomic,strong) UIImageView *noDataImgView;
@end

@implementation TeachSimulationDeatilController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[CoachTabBarController shareLZHTabbarController].tabBar setHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createSubView];
    [self loadData];
}

#pragma mark ==========加载数据==========
- (void)loadData{
    [self showLoading];
    [[NetworkEngine sharedClient] GETByToken:TEACHSIMULATIONLIST data:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInteger:self.sbujectType],@"subjectTyp", nil] success:^(id responseObject) {
        [self hideHUD];
        if ([[responseObject objectForKey:@"code"] integerValue]==1) {
            if ([[responseObject objectForKey:@"data"] count]==0) {
                self.tableView.hidden = YES;
                self.noDataImgView.hidden = NO;
            } else {
                if (self.tableView.hidden == YES) {
                    self.noDataImgView.hidden = YES;
                    self.tableView.hidden = NO;
                }
                self.dataDic = [NSMutableDictionary dictionaryWithDictionary:[responseObject objectForKey:@"data"]];
            }
            [self.tableView reloadData];
        } else if ([[responseObject objectForKey:@"code"] integerValue]==2) {
            [self toLoginVC];
        } else {
            [self showMessage:[responseObject objectForKey:@"msg"]];
        }
    } failure:^(NSString *error) {
        [self showError:error];
    }];
}

#pragma mark ==========创建视图==========
- (void)createSubView{
    self.title = @"模拟考试";
    [self setNavBack];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.noDataImgView];
    [self.view addSubview:self.tableView];
}

#pragma mark ==========懒加载==========
- (UIImageView *)noDataImgView{
    if (!_noDataImgView) {
        _noDataImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 179*2*SCALEUNIT, 140*2*SCALEUNIT)];
        _noDataImgView.image = [UIImage imageNamed:@"CoachNoDataImg"];
        _noDataImgView.center = CGPointMake(self.view.frame.size.width/2.0,(self.view.frame.size.height-SYSTEM_TOP-120*SCALEUNIT)/2.0-SYSTEM_TOP*2);
        _noDataImgView.hidden = YES;
    }
    return _noDataImgView;
}
- (UITableView *)tableView{
    
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-SYSTEM_TOP-120*SCALEUNIT) style:UITableViewStylePlain];
        _tableView.tableFooterView = [[UIView alloc]init];
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#eeeeee"];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 30*SCALEUNIT, 0, 30*SCALEUNIT)];
        [_tableView setSeparatorColor:[UIColor colorWithHexString:@"#eeeeee"]];
        [_tableView registerClass:[TeachSimulationCell class] forCellReuseIdentifier:@"TeachSimulationCell"];
        
        WS(ws);
        _tableView.mj_header = [CustomGifHeader headerWithRefreshingBlock:^{
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [ws loadData];
                [ws.tableView.mj_header endRefreshing];
            });
        }];
    }
    return _tableView;
}

#pragma mark ==========UITableView代理方法==========
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataDic.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[self.dataDic objectForKey:[[self.dataDic allKeys] objectAtIndex:section]] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 380*SCALEUNIT;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TeachSimulationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TeachSimulationCell" forIndexPath:indexPath];
    if (self.dataDic != nil) cell.dataDic = [[self.dataDic objectForKey:[[self.dataDic allKeys] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ExamPlayListModel *tempModel = [ExamPlayListModel mj_objectWithKeyValues:[[self.dataDic objectForKey:[[self.dataDic allKeys] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]];
    if (tempModel.curStatus==3) {
        //tempModel.ID = @"217120523493048320";
        ExamPlayInfoController *tempController = [[ExamPlayInfoController alloc] init];
        [tempController setSubjectType:self.sbujectType==2?@"2":@"3"];
        [tempController setDetail:tempModel];
        self.block(tempController);
    } else {
        [self showMessage:@"您选择的考试尚未出成绩"];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 110*SCALEUNIT;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *backView = [[UIView alloc] init];
    backView.backgroundColor = [UIColor colorWithHexString:@"#eeeeee"];
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(25*SCALEUNIT, 0, 300*SCALEUNIT, 110*SCALEUNIT)];
    headerLabel.textAlignment = NSTextAlignmentLeft;
    headerLabel.font = [UIFont systemFontOfSize:LoginFont-3];
    headerLabel.text = [[self.dataDic allKeys] objectAtIndex:section];
    [backView addSubview:headerLabel];
    return backView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
